package com.itheima.day06.sort_bubble;

import java.util.Scanner;

import static java.util.Arrays.binarySearch;

public class ErfenChazhao {
    public static void main(String[] args) {
        int[] arr = {10, 5, 6, 8, 9, 2, 1, 3, 4, 7};
        TestXuanze.compare1(arr);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的数字：");
        int number = sc.nextInt();
//        int index = binarySearch(arr, number);
//        上面方法为Arrays类中的查找方法，但是这个方法只能对有序的数组进行查找
        int result = chazhao(arr, number);
        if(result==-1){
            System.out.println("没有找到");
        }else{
            System.out.println("找到了，索引为："+result);
        }
    }

    private static int chazhao(int[] arr, int number) {
        int left = 0;
        int right = arr.length - 1;
        int mid = (left+right)/2;
        while (left <= right){
            if(arr[mid]== number){
                return mid;
            }
            if(number > arr[mid]){
                left = mid+1;
                mid = (left+right)/2;
            }else if(number < arr[mid]){
                right = mid-1;
                mid = (left+right)/2;
            }
        }
        return -1;

    }


}
